from sqlalchemy import BIGINT, Column, Index, String, text, ForeignKey, Date, INTEGER, DateTime, \
	DECIMAL, TIMESTAMP, SMALLINT, Text
from sqlalchemy.dialects.mysql.types import TINYINT, LONGTEXT
from sqlalchemy.orm import relationship
from db import Base


metadata = Base.metadata


class TCarRawinfo(Base):
    __tablename__ = 't_car_rawinfo'
    __table_args__ = (
        Index('ind_4', 'licenseNo', 'vehicleFrameNo'),
        Index('uni1', 'licenseNo', 'vehicleFrameNo', 'engine_code', 'model', 'register_date', unique=True)
    )

    id = Column(BIGINT(), primary_key=True)
    licenseNo = Column(String(20), nullable=False, index=True, server_default=text("'?'"))
    vehicleFrameNo = Column(String(100), nullable=False)
    engine_code = Column(String(100), nullable=False)
    model = Column(String(100))
    brand = Column(String(100))
    register_date = Column(String(20))
    owner_name = Column(String(40), server_default=text("''"))
    owner_code = Column(String(100), index=True, server_default=text("''"))
    owner_mobile = Column(String(20), index=True, server_default=text("''"))
    trans_date = Column(String(20))
    is_trans = Column(String(20))
    brandCode = Column(String(50), server_default=text("''"))
    address = Column(String(255), server_default=text("''"))
    biDate = Column(String(20), server_default=text("''"))


class TCarInsurance(Base):
    __tablename__ = 't_car_insurance'
    __table_args__ = (
        Index('licenseNo', 'licenseNo', 'frameNo', 'engineNo'),
    )

    id = Column(BIGINT(), primary_key=True)
    licenseNo = Column(String(10), nullable=False)
    frameNo = Column(String(32), nullable=False)
    engineNo = Column(String(32), nullable=False)
    biOrderNumber = Column(String(32))
    ciOrderNumber = Column(String(32))
    biDate = Column(String(32), nullable=False)
    ciDate = Column(String(32), nullable=False)
    insuranceUnit = Column(String(32), nullable=False)
    insuranceContent = Column(String(), nullable=False)


class TInsuranceType(Base):
    __tablename__ = 't_insurance_types'

    id = Column(INTEGER(), primary_key=True)
    code = Column(String(80), nullable=False)
    type_name = Column(String(80), nullable=False)
    amount_range = Column(String(255))
    description = Column(String(255))
    status = Column(INTEGER())


class TInsuranceUnit(Base):
    __tablename__ = 't_insurance_unit'

    id = Column(INTEGER(), primary_key=True)
    unit_code = Column(String(255), nullable=False)
    unit_name = Column(String(255), nullable=False)
    interface_count = Column(INTEGER(), nullable=False)
    status = Column(TINYINT(4), nullable=False, server_default=text("'0'"))
    create_date = Column(Date, nullable=False)
    valid_time = Column(Date)
    applicationID = Column(String(32), nullable=False)
    thpBizID = Column(String(32), nullable=False)
    sign = Column(String(500))
    comment = Column(String(400))
    logoURL = Column(String(255))
    joinStatus = Column(TINYINT())
    insure_type = Column(String(45))


class TChannel(Base):
    __tablename__ = 't_channel'

    app_id = Column(String(25), primary_key=True)
    app_name = Column(String(25), nullable=False)


class TProvince(Base):
    __tablename__ = 't_province'

    ProvinceID = Column(INTEGER(), primary_key=True)
    ProvinceName = Column(String(40), nullable=False)
    ProvinceBriefName = Column(String(3), nullable=False)
    gbcode = Column(String(6))
    pinyincode = Column(String(8))


class TCity(Base):
    __tablename__ = 't_city'

    CityID = Column(INTEGER(), primary_key=True)
    CityName = Column(String(40), nullable=False)
    ZipCode = Column(String(50))
    ProvinceID = Column(ForeignKey('t_province.ProvinceID'), index=True)
    gbcode = Column(String(6), index=True)
    cnlifecode = Column(String(7))
    pinyincode = Column(String(8))
    cityPlate = Column(String(20))

    t_province = relationship('TProvince')


class TAreaInsuranceUnit(Base):
    __tablename__ = 't_area_insurance_unit'
    __table_args__ = (
        Index('provinceID', 'provinceID', 'cityID', 'insuranceUnitId', 'app_id', unique=True),
    )

    id = Column(INTEGER(), primary_key=True)
    provinceID = Column(ForeignKey('t_province.ProvinceID'), nullable=False, index=True)
    cityID = Column(ForeignKey('t_city.CityID'), index=True)
    insuranceUnitId = Column(ForeignKey('t_insurance_unit.id'), nullable=False, index=True)
    status = Column(TINYINT(3), nullable=False, server_default=text("'1'"))
    app_id = Column(ForeignKey('t_channel.app_id'), index=True)

    app = relationship('TChannel')
    t_city = relationship('TCity')
    t_insurance_unit = relationship('TInsuranceUnit')
    t_province = relationship('TProvince')


class TCommissionRate(Base):
    __tablename__ = 't_commission_rate'
    __table_args__ = (
        Index('ins_corp_id', 'ins_corp_id', 'city_code', 'app_id', unique=True),
    )

    id = Column(INTEGER(), primary_key=True)
    ins_corp_id = Column(ForeignKey('t_insurance_unit.id'), nullable=False, index=True)
    rate1 = Column(DECIMAL(5, 2), nullable=False)
    rate2 = Column(DECIMAL(5, 2), nullable=False)
    rate3 = Column(DECIMAL(5, 2), nullable=False)
    start_datetime = Column(DateTime, nullable=False)
    update_datetime = Column(DateTime, nullable=False)
    user_id = Column(BIGINT(), nullable=False)
    status = Column(INTEGER())
    rate_comp = Column(DECIMAL(5, 2))
    city_code = Column(String(6))
    app_id = Column(ForeignKey('t_channel.app_id'), index=True)

    app = relationship('TChannel')
    ins_corp = relationship('TInsuranceUnit')


class TOpenOrderKernel(Base):
    __tablename__ = 't_open_order_kernel'
    __table_args__ = (
        Index('UNIQUE_1', 'serviceNo', 'status', unique=True),
    )

    id = Column(BIGINT(), primary_key=True)
    biPremium = Column(String(15), server_default=text("'0'"))
    ciPremium = Column(String(15), server_default=text("'0'"))
    carshipTax = Column(String(15), server_default=text("'0'"))
    totalCost = Column(String(15), server_default=text("'0'"))
    payAmount = Column(String(15), server_default=text("'0'"))
    orderTime = Column(TIMESTAMP, nullable=False, server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))
    biProposalNo = Column(String(32), server_default=text("'0'"))
    ciProposalNo = Column(String(32), server_default=text("'0'"))
    serviceNo = Column(String(50), index=True, server_default=text("'0'"))
    status = Column(SMALLINT(), index=True, server_default=text("'0'"))
    payState = Column(TINYINT(1), server_default=text("'0'"))
    appId = Column(String(32), index=True, server_default=text("''"))
    rebates = Column(String(15), server_default=text("'0'"))
    msg = Column(String(500), server_default=text("''"))
    commission_status = Column(TINYINT(1), server_default=text("'1'"))


class TOpenInsureinfoKernel(Base):
    __tablename__ = 't_open_insureinfo_kernel'

    id = Column(BIGINT(), primary_key=True)
    ciStartDate = Column(String(15), server_default=text("'0'"))
    biStartDate = Column(String(15), server_default=text("'0'"))
    biPolicyNo = Column(String(32), server_default=text("'0'"))
    ciPolicyNo = Column(String(32), server_default=text("'0'"))
    insureCode = Column(String(15), server_default=text("'0'"))
    insureCity = Column(String(15), server_default=text("'0'"))
    ownerName = Column(String(32), server_default=text("'0'"))
    ownerId = Column(String(20), server_default=text("'0'"))
    ownerMobile = Column(String(20), server_default=text("'0'"))
    vinNo = Column(String(20), server_default=text("'0'"))
    engineNo = Column(String(20), server_default=text("'0'"))
    model = Column(String(32), server_default=text("'0'"))
    licenseNo = Column(String(15), server_default=text("'0'"))
    registerDate = Column(TIMESTAMP, nullable=False,
                          server_default=text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"))
    isTrans = Column(TINYINT(1), server_default=text("'0'"))
    transDate = Column(TIMESTAMP, server_default=text("'0000-00-00 00:00:00'"))
    content = Column(Text)
    insuredInfo = Column(String(255), server_default=text("'0'"))
    policyholder = Column(String(255), server_default=text("'0'"))
    addresseeName = Column(String(32))
    addresseeMobile = Column(String(15))
    addresseeProvince = Column(String(10))
    addresseeCity = Column(String(10))
    addresseeCounty = Column(String(10))
    addresseeDetails = Column(String(100))
    expressNo = Column(String(32))
    expressCompanyName = Column(String(32))
    t_open_order_id = Column(ForeignKey('t_open_order_kernel.id'), index=True)

    t_open_order = relationship('TOpenOrderKernel')


class TInsuranceTypeList(Base):
    __tablename__ = 't_insurance_type_list'

    id = Column(INTEGER(), primary_key=True)
    riskCode = Column(String(135))
    riskName = Column(String(135))
    insuredAmount = Column(String(600))
    status = Column(String(135))
    insuranceCode = Column(String(135))
    insur_type_code = Column(String(135))


class TUserTypeDictionary(Base):
    __tablename__ = 't_user_type_dictionary'

    id = Column(INTEGER(), primary_key=True)
    typename = Column(String(80), nullable=False)
    comment = Column(String(255))


class TUserprofile(Base):
    __tablename__ = 't_userprofile'

    id = Column(BIGINT(), primary_key=True)
    user_name = Column(String(40))
    pwd = Column(String(255), nullable=False)
    type_id = Column(ForeignKey('t_user_type_dictionary.id'), nullable=False, index=True)
    register_time = Column(DateTime, nullable=False)
    is_admin = Column(TINYINT(1), nullable=False, server_default=text("'1'"))
    is_enabled = Column(TINYINT(1), nullable=False, server_default=text("'0'"))
    is_locked = Column(TINYINT(1), nullable=False, server_default=text("'0'"))
    mobile = Column(String(20), unique=True)
    phone = Column(String(40))
    email = Column(String(80), index=True)
    tag = Column(String(255))
    point = Column(BIGINT(), server_default=text("'0'"))
    rank = Column(String(20))
    is_unit = Column(TINYINT(4), server_default=text("'0'"))
    photo_url = Column(String(255))
    wenxin = Column(String(255))
    QQ = Column(String(255))
    blog = Column(String(255))
    alias_name = Column(String(255))
    IsRealName = Column(TINYINT(4))
    IsInfoReady = Column(TINYINT(4), server_default=text("'0'"))
    default_unit_id = Column(BIGINT())
    pinyincode = Column(String(20))
    onelevel_count = Column(INTEGER(), index=True, server_default=text("'0'"))
    twolevel_count = Column(INTEGER(), index=True, server_default=text("'0'"))
    threelevel_count = Column(INTEGER(), index=True, server_default=text("'0'"))

    type = relationship('TUserTypeDictionary')


class TRolesGroup(Base):
    __tablename__ = 't_roles_group'

    id = Column(INTEGER(), primary_key=True)
    type_name = Column(String(80), nullable=False)
    description = Column(String(255))


class TRole(Base):
    __tablename__ = 't_role'

    id = Column(BIGINT(), primary_key=True)
    role_name = Column(String(32), nullable=False, unique=True)
    role_code = Column(String(80), nullable=False)
    group_id = Column(ForeignKey('t_roles_group.id'), index=True)
    islocked = Column(TINYINT(4), nullable=False, server_default=text("'0'"))
    description = Column(String(256))

    group = relationship('TRolesGroup')


class TUserRole(Base):
    __tablename__ = 't_user_role'
    __table_args__ = (
        Index('Index_user_role', 'role_id', 'user_id', unique=True),
    )

    id = Column(BIGINT(), primary_key=True)
    role_id = Column(ForeignKey('t_role.id'), nullable=False)
    user_id = Column(ForeignKey('t_userprofile.id'), nullable=False, index=True)

    role = relationship('TRole')
    user = relationship('TUserprofile')


class TAccount(Base):
    __tablename__ = 't_account'

    id = Column(BIGINT(), primary_key=True)
    user_id = Column(ForeignKey('t_userprofile.id'), nullable=False, index=True)
    unit_id = Column(BIGINT())
    balance = Column(DECIMAL(15, 2), nullable=False)
    cash = Column(DECIMAL(15, 2))
    bank_card1 = Column(String(40))
    bank1_code = Column(INTEGER())
    bank_card2 = Column(String(40))
    bank2_code = Column(INTEGER())
    bank_card3 = Column(String(40))
    bank3_code = Column(INTEGER())
    pay_password = Column(String(80), nullable=False)
    isLocked = Column(TINYINT(4), nullable=False, server_default=text("'0'"))
    bank_card1_img_url = Column(String(255))
    bank_card2_img_url = Column(String(255))
    bank_card3_img_url = Column(String(255))

    user = relationship('TUserprofile')


class TCustomerInfo(Base):
    __tablename__ = 't_customer_info'

    id = Column(BIGINT(), primary_key=True)
    user_id = Column(ForeignKey('t_userprofile.id'), index=True)
    customer_name = Column(String(40), nullable=False)
    doc_code = Column(String(255), nullable=False)
    doc_type_id = Column(ForeignKey('t_doc_types.id'), nullable=False, index=True)
    driverNum = Column(String(40))
    drivateDate = Column(Date)
    isImmevalid = Column(TINYINT(4), nullable=False, server_default=text("'1'"))
    is_enabled = Column(TINYINT(1), nullable=False, server_default=text("'0'"))
    is_locked = Column(TINYINT(1), nullable=False, server_default=text("'0'"))
    mobile = Column(String(20), nullable=False, index=True)
    phone = Column(String(40))
    email = Column(String(80), index=True)
    point = Column(BIGINT())
    rank = Column(String(20))
    car_insurance_id = Column(BIGINT())
    owner_id = Column(ForeignKey('t_userprofile.id'), nullable=False, index=True)
    create_date = Column(DateTime, nullable=False)
    lastupdateddateTime = Column(DateTime)
    photo_url = Column(String(255))
    wenxin = Column(String(255))
    QQ = Column(String(255))
    blog = Column(String(255))
    alias_name = Column(String(255))
    IsRealName = Column(TINYINT(4))
    tag = Column(String(255))
    receive_rec_id = Column(BIGINT())
    pinyincode = Column(String(20))
    province = Column(String(12), index=True)
    city = Column(String(12), index=True)
    county = Column(String(12))
    gender = Column(TINYINT(4), index=True)
    birthday = Column(Date, index=True)
    identity_front_url = Column(String(80), server_default=text("''"))
    identity_back_url = Column(String(80), server_default=text("''"))

    doc_type = relationship('TDocType')
    owner = relationship('TUserprofile', primaryjoin='TCustomerInfo.owner_id == TUserprofile.id')
    user = relationship('TUserprofile', primaryjoin='TCustomerInfo.user_id == TUserprofile.id')


class TDocType(Base):
    __tablename__ = 't_doc_types'

    id = Column(INTEGER(), primary_key=True)
    type_name = Column(String(80), nullable=False)
    description = Column(String(255))


class TCarType(Base):
    __tablename__ = 't_car_types'

    id = Column(INTEGER(), primary_key=True)
    type_name = Column(String(80), nullable=False)
    vehicleFgwCode = Column(String(50), nullable=False)
    brandCode = Column(String(36), nullable=False, index=True)
    brandName = Column(String(50), nullable=False)
    engineDesc = Column(String(10), nullable=False)
    familyName = Column(String(100), nullable=False)
    gearboxType = Column(String(100))
    remark = Column(String(255))
    newCarPrice = Column(String(11))
    purchasePriceTax = Column(String(11))
    importFlag = Column(String(1), nullable=False, server_default=text("'0'"))
    purchasePrice = Column(String(18))
    seat = Column(String(50))
    standardName = Column(String(100))
    msg = Column(String(100))



class TCarprofile(Base):
    __tablename__ = 't_carprofile'
    __table_args__ = (
        Index('carprofile_index1', 'owner_id', 'licenseNo', 'vehicleFrameNo'),
        Index('carprofile_index2', 'licenseNo', 'vehicleFrameNo')
    )

    id = Column(BIGINT(), primary_key=True)
    owner_id = Column(ForeignKey('t_customer_info.id'), nullable=False, index=True)
    noLicenseFlag = Column(TINYINT(4), nullable=False, server_default=text("'1'"))
    licenseNo = Column(String(20), nullable=False, index=True, server_default=text("'?'"))
    cityPlate = Column(String(20), nullable=False)
    vehicleFrameNo = Column(String(255), nullable=False, index=True)
    engine_code = Column(String(255), nullable=False)
    vehicleModel_id = Column(ForeignKey('t_car_types.id'), nullable=False, index=True)
    config_model = Column(String(255))
    register_date = Column(Date, nullable=False)
    isLoanCar = Column(TINYINT(4), nullable=False, server_default=text("'0'"))
    specialCarFlag = Column(TINYINT(4), nullable=False, server_default=text("'0'"))
    specialCarDate = Column(Date)
    vehicleInvoiceNo = Column(Text)
    vehicleInvoiceDate = Column(Date)
    status = Column(TINYINT(4), nullable=False, server_default=text("'0'"))
    district_code = Column(INTEGER())
    city_code = Column(INTEGER(), nullable=False)
    state_code = Column(INTEGER())
    country_code = Column(INTEGER())
    latitude = Column(DECIMAL(10, 6))
    longitude = Column(DECIMAL(10, 6))
    createtime = Column(DateTime, nullable=False)
    lastupdateddateTime = Column(DateTime)
    create_user_id = Column(BIGINT(), nullable=False)
    description = Column(String(255))
    licenseNo_img_URL = Column(String(255))
    driving_license_img_URL = Column(String(255))
    issueLicenseDate = Column(Date)
    comm_end_date = Column(Date)
    compulsory_end_date = Column(Date)
    driving_license_img_back_URL = Column(String(255))

    owner = relationship('TCustomerInfo')
    vehicleModel = relationship('TCarType')


class TInsuranceQueryList(Base):
    __tablename__ = 't_insurance_query_list'
    __table_args__ = (
        Index('t_qry_index1', 'return_service_no'),
        Index('t_qry_index2', 'user_id'),
        Index('t_qry_index3', 'car_id'),
        Index('t_qry_index4', 'customer_id'),
        Index('t_qry_index5', 'user_id', 'return_service_no'),
    )

    id = Column(BIGINT(), primary_key=True)
    carId = Column(BIGINT(), nullable=False)
    insUnitId = Column(ForeignKey('t_insurance_unit.id'), nullable=False, index=True)
    customerId = Column(BIGINT(), nullable=False)
    userId = Column(BIGINT(), nullable=False)
    packageType = Column(LONGTEXT(), nullable=False)
    isValid = Column(TINYINT(), nullable=False)
    totalCost = Column(DECIMAL(), nullable=False)
    cityId = Column(BIGINT(), nullable=False)
    districtId = Column(BIGINT(), nullable=False)
    queryTime = Column(DateTime(), nullable=False)
    biProposalNo = Column(String(), nullable=True)
    ciProposalNo = Column(String(), nullable=True)
    ciBeginDate = Column(Date(), nullable=True)
    ciEndDate = Column(Date(), nullable=True)
    biBeginDate = Column(Date(), nullable=True)
    biEndDate = Column(Date(), nullable=True)
    qryServiceNo = Column(String(), nullable=True)
    bizId = Column(String(), nullable=True)
    source = Column(String(), nullable=True)
    discount = Column(DECIMAL(), nullable=True)
    status = Column(INTEGER(), nullable=True)
    errorMsg = Column(String(), nullable=True)


class TInsuranceQueryCostList(Base):
    __tablename__ = 't_ins_qry_cost_list'

    id = Column(BIGINT(), primary_key=True)
    queryId = Column(ForeignKey('t_insurance_query_list.id'), nullable=False, index=True)
    cost = Column(DECIMAL(), nullable=False)
    insCatId = Column(ForeignKey('t_insurance_types.id'), nullable=False, index=True)
    warrantyCost = Column(DECIMAL(), nullable=True)